package logger

import (
	"log"
	"os"
	"sync"
	"time"
)

type Logger struct {
	logger *log.Logger
}

var (
	in1t   sync.Once
	logger *Logger
	file   *os.File
	prefix string
)

func init() {
	file, err := os.OpenFile(time.Now().Format("2006-01-02")+".log", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0777)

	if err != nil {
		panic(err.Error())
	}

	SetSaveFile(file)
	GetLogger().PrintInfo("Init Logger System")
}

//设置存储位置
func SetSaveFile(lFile *os.File) {
	file = lFile
}

//单例构建唯一日志桩
func GetLogger() *Logger {
	in1t.Do(func() {
		logger = &Logger{}
		logger.logger = log.New(file, prefix, log.LstdFlags)
	})
	return logger
}

//输出一条日志
func (logst Logger) PrintLog(log LOG) {
	logst.logger.SetPrefix(log.Type.String())
	logst.logger.Println(log.Value)
}

//标准日志 INFO
func (logst Logger) PrintInfo(value string) {
	var log LOG
	log.Type = INFO
	log.Value = value
	logst.PrintLog(log)
}

//标准日志 WARN
func (logst Logger) PrintWarn(value string) {
	var log LOG
	log.Type = WARN
	log.Value = value
	logst.PrintLog(log)
}

/**
* 非标准日志 ERROR
* 在输出错误之后会输出堆栈信息 并panic程序
**/
func (logst Logger) PrintError(value string) {
	var log LOG
	log.Type = ERROR
	log.Value = value
	logst.PrintLog(log)
	panic(value)
}
